package net.projects.pro5_rss.bean;

import org.apache.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.ParserFactory;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class RSSDataCapturer extends DefaultHandler {
    private final Logger log= Logger.getLogger(RSSDataCapturer.class.getName());
    private List<CommonInformationItem> list;
    private CommonInformationItem item;
    private String source;
    private StringBuffer buffer;
    private DateFormat df=new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);

    public RSSDataCapturer(String name) {
        this.source = name;
        buffer=new StringBuffer();
    }

    public List<CommonInformationItem> load(String sourceUrl) {

        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            saxParser.parse(sourceUrl, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    @Override
    public void startDocument() throws SAXException {
        log.debug("开始解析");
        list = new ArrayList<>();
        super.startDocument();
    }

    @Override
    public void endDocument() throws SAXException {
        log.info("结束解析");
        super.endDocument();
    }

    private int status=0;
    private final int STATUS_TITLE=1;
    private final int STATUS_LINK=2;
    private final int STATUS_DESCRIPTION=3;
    private final int STATUS_PUBDATE=4;
    private final int STATUS_GUID=5;
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        log.debug("元素的开头:"+uri+",localName:"+localName+",qName:"+qName);

        switch (qName.toLowerCase()) {
            case "item":
                item = new CommonInformationItem();
                item.setSource(source);
                break;
            case "title":
                status=STATUS_TITLE;
                break;
            case "link":
                status=STATUS_LINK;
                break;
            case "description":
                status=STATUS_DESCRIPTION;
                break;
            case "pubdate":
                status=STATUS_PUBDATE;
                break;
            case "guid":
                status=STATUS_GUID;
                break;
        }
    }


    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        log.debug("元素的结尾:"+uri+",localName:"+localName+",qName:"+qName);
        if (item == null) {
            return;
        }
        switch (qName.toLowerCase()) {
            case "item":
                if (item.getId()==null){
                    item.setId(item.getDescription().hashCode()+"");
                }
                list.add(item);
                item = null;
                break;
            case "title":
                item.setTitle(buffer.toString());
                status=0;
                break;
            case "link":
                item.setLink(buffer.toString());
                status=0;
                break;
            case "description":
                item.setDescription(buffer);
                status=0;
                break;
            case "pubdate":
                item.setTxtDate(buffer.toString());
                try {
                  item.setDate(df.parse(buffer.toString().trim()));
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                status=0;
                break;
            case "guid":
                item.setId(buffer.toString());
                status=0;
                break;
        }
        buffer=new StringBuffer();
    }
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String txt=new String(ch,start,length);
        log.debug("记录到了文字:"+txt);
        buffer.append(txt);
    }
}
